package com.hotel.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;

import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.ChargeLogic;
import com.hotel.BL.FolioLogic;
import com.hotel.BL.PaymentLogic;
import com.hotel.BL.SubPaymentLogic;

import data.Charge;
import data.Folio;
import data.Payment;
import data.SubPayment;
import data.passingData.FolioData;
import data.passingData.FoliotableData;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class FolioService extends HttpServlet {
	//http://localhost:8080/folioService?func=addFolio&title=FolioA
	//http://localhost:8080/folioService?func=editFolio&FolioId=828&title=FolioAB
	//http://localhost:8080/folioService?func=delFolio&FolioId=828
	//http://localhost:8080/folioService?func=loadFolioList&FolioId=828
	//http://localhost:8080/folioService?func=createFolioTable&folioId=828
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addFolio")){
				AddFolio(req,resp);
			}
			else if(doClass.equals("loadFolioList")){
				LoadFolioList(req,resp);
			}
			else if(doClass.equals("deleteFolioList")){
				DeleteFolioList(req,resp);
			}
			else if(doClass.equals("getStatus")){
				getFoilo(req,resp);
			}
			else if(doClass.equals("loadSingleFolioList")){
				LoadSingleFolioList(req,resp);
			}
			else if(doClass.equals("routenewFolio")){
				RoutenewFolio(req,resp);
			}
			
			else if(doClass.equals("editChargeInFolio")){
				EditChargeInFolio(req,resp);
			}
			else if(doClass.equals("createFolioTable")){
				CreateFolioTable(req,resp);
			}
			else if(doClass.equals("LockTheFolio")){
				LockTheFolio(req,resp);
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	private void AddFolio(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{		
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String chargeRes = req.getParameter("chargeres");
		String resvId = req.getParameter("resId");
		String chargeGroup =req.getParameter("chargegroup");
		String chargeGroupId=req.getParameter("chargegid");
		boolean isLock = false;
		List<FolioData> obj = new FolioLogic(req,resp).AddFolio( hotelId,chargeRes,resvId,isLock,chargeGroup,chargeGroupId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private	void LoadFolioList(HttpServletRequest req, HttpServletResponse resp) throws IOException, EntityNotFoundException
	{
		String groupId = req.getParameter("id");
	
		List<List< ArrayList<FolioData>>> f = new FolioLogic(req,resp).LoadFolioList(groupId);
		Gson gson = new Gson();
	resp.setContentType("text/plain; charset=UTF-8");
	resp.getWriter().println(gson.toJson(f));
	}

	private	void LoadSingleFolioList(HttpServletRequest req, HttpServletResponse resp) throws IOException, EntityNotFoundException
	{
		String resId = req.getParameter("resId");
		List<List< ArrayList<FolioData>>>  f = new FolioLogic(req,resp).LoadSingleFolioList(resId);
		Gson gson = new Gson();

	
	resp.setContentType("text/plain; charset=UTF-8");
	resp.getWriter().println(gson.toJson(f));
		
		
	}
	
	private	void DeleteFolioList(HttpServletRequest req, HttpServletResponse resp) throws IOException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String groupId = req.getParameter("gid");
		String resid = req.getParameter("resid");
		String resFolioList = req.getParameter("resFolioList");
		String groupFolioList = req.getParameter("groupFolioList");
		boolean f = new FolioLogic(req,resp).DeleteFolio(groupId, resid, resFolioList, groupFolioList);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(f));
	}
	private	void getFoilo(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		Gson gson = new Gson();
		String folioId = req.getParameter("folioId");
		String[]folioIdArray = folioId.trim().split(",");
		FolioLogic fLogic = new FolioLogic(req,resp);
		Folio g = new Folio();
		for(int y=0;y<folioIdArray.length;y++)
		{
			if(!folioIdArray[y].equals(""))
			{
				g = fLogic.GetFolio(Long.parseLong(folioIdArray[y]));	
			}
		}
		resp.setContentType("text/plain; charset=UTF-8");
		if (g != null) {
			System.out.println(gson.toJson(g));
			resp.getWriter().println(gson.toJson(g));
		} else {
			resp.getWriter().println(gson.toJson(g));
		}
		
	}
	
	private	void RoutenewFolio(HttpServletRequest req, HttpServletResponse resp) throws IOException, EntityNotFoundException
	{ 
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String groupId = req.getParameter("gid");
		long resId = Long.parseLong(req.getParameter("resId"));
		String chargeList = req.getParameter("chargeList");
		String resEditchargeId = req.getParameter("resEditchargeId");
		String Editchargegid =req.getParameter("Editchargegid");
		long FolioId = Long.parseLong(req.getParameter("folioId"));
		String resListString ="";
		String groupListString ="";
		if(resId == 0)
		{
			resListString=resEditchargeId;
		}
		else if(resEditchargeId == null||resEditchargeId.isEmpty())
		{
			
			resListString=String.valueOf(resId);
		}
		else
		{
			resListString =resEditchargeId+","+resId;
		}
		
		if(!groupId.equals("0"))
		{
			groupListString=groupId;
		}else if(!Editchargegid.isEmpty()&&Editchargegid!=null)
		{
			groupListString=Editchargegid;
		}else
		{
			groupListString=Editchargegid+","+groupId;
		}
		FolioLogic folioLogic = new FolioLogic(req,resp);
		double f = folioLogic.RoutetoNewFolio(hotelId, chargeList, groupId, resId, FolioId);
		folioLogic.DeleteChargeRoute(resEditchargeId,Editchargegid,chargeList);
		f = folioLogic.EditCgargeInRes(resListString, groupListString);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(f));
	}

	private void EditChargeInFolio(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long folioId = Long.parseLong(req.getParameter("folioId"));
		String chargeIdString = req.getParameter("chargeId");
		Object obj = new FolioLogic(req,resp).EditChargeInFolio(folioId,chargeIdString);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

	private void CreateFolioTable(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		String folioId = req.getParameter("folioId");
		ChargeLogic chargeLogic = new ChargeLogic(req,resp);
		PaymentLogic paymentLogic = new PaymentLogic(req,resp);
		SubPaymentLogic subPaymentLogic = new SubPaymentLogic(req,resp);
	if(folioId != null)
	{  
		List<Charge> cList =new ArrayList<Charge>();
		List<FoliotableData> pList = new ArrayList<FoliotableData>();
		
		long fid = Long.parseLong(folioId);
		Folio f = new FolioLogic(req,resp).GetFolio(fid);
		if(!f.getChargeString().equals("") && f.getChargeString() != null)
		{
			String [] chargeList =f.getChargeString().trim().split(",");
			for(int n= 0;n<chargeList.length;n++)
			{
				Charge c = chargeLogic.GetCharge(Long.parseLong(chargeList[n]));
				cList.add(c);
			}
		}
	
		if(f.getPaymentString()!= null)
		{
			String [] paymentList = f.getPaymentString().trim().split(",");
			for(int n= 0;n<paymentList.length;n++)
			{
				FoliotableData  ft = new FoliotableData();
				SubPayment p = subPaymentLogic.GetSubPayment(Long.parseLong(paymentList[n]));
				ft.setAmount(p.getAmount()); 
				ft.setId(p.getId());
				Payment pp = paymentLogic.GetPayment(Long.parseLong(p.getParentPaymentString()));
				ft.setDescription(pp.getDescription());
				ft.setPayTime(pp.getPayDate());
				
				pList.add(ft);
			}
		}
	
	
	
		List<Object> list = new ArrayList<Object>();	
		list.add(cList);
		list.add(pList);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(list));
	}
	
	}
	
	private void LockTheFolio(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long folioId = Long.parseLong(req.getParameter("folioId"));
		HashMap<String,String> obj = new FolioLogic(req,resp).Lockfolio(folioId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
}
